#!/usr/bin/env python3
# -*- coding: utf-8 -*-
##############################################
# @Author: DengLibin 榆霖
# @Date: Create in 2022-03-07 10:30:19
# @Description: 访问mysql
##############################################
'module name'
__author__ = 'DengLibin'


# 安装MySQL驱动
# 由于MySQL服务器以独立的进程运行，并通过网络对外服务，所以，需
# 要支持Python的MySQL驱动来连接到MySQL服务器。MySQL官方提供了mysql-connector-python驱动，
# pip install mysql-connector-python 

# 如果上面的命令安装失败，可以试试另一个驱动：
# $ pip install mysql-connector

from pymysql import connect
from datetime import datetime
if __name__ == '__main__':
    try:
        conn = connect(
            host = '192.168.1.159',
            port = 3306,
            user = 'root',
            password = 'denglibin',
            db = 'denglibin',
            charset = 'utf8'
        )
        cursor = conn.cursor()
    except Exception as e:
        print(e)
    else:
        print('Connect Success:%s' % cursor)
    # 插入数据 
    now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    sql = "insert into user(id, name, age, create_date) values(1, '张三', 34, '%s')" % now
    cursor.execute(sql)
    conn.commit()
    conn.close()
    
        


